package ServletDemo;

import java.sql.*;

public class Datebase {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        //加载驱动
//        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//        //数据库URL[即：是要连接那一个数据库]localhost--指定本机；integratedSecurity=true--指定windows验证模式连接
//        String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";//windows集成模式连接
////        String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login";
////        String username = "hh";
////        String passWord = "123";
////        Connection comm = DriverManager.getConnection(url,username,passWord);
//        //建立连接
//        Connection comm = DriverManager.getConnection(url);
//        String SQL = "SELECT * FROM [user] ";//注意：表名一定要带[]
//        Statement stmt = comm.createStatement();
//        //结果集
//        ResultSet rs = stmt.executeQuery(SQL);
//
//        while(rs.next()){
//            System.out.println(rs.getString(1)+""+rs.getString(2));
//        }
//        rs.close();
//        comm.close();
        //************************************************************************************************************************
        //测试
        String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Login;integratedSecurity=true;";
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection comm = DriverManager.getConnection(url);
        String sql = "select * from Book";
        System.out.println("a");
        Statement st = comm.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + " " + rs.getString(2));
        }
        //5 执行sql语句
        int temp = st.executeUpdate(sql);
        System.out.println("返回的值：" + temp);
        rs.close();
        //6 关闭sql对象
        st.close();
        //7 关闭连接
        comm.close();
        //************************************************************************************************************************
    }
}
